/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;


import coms.dto.Heading;
import coms.dto.Rating;
import coms.dto.Section;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author Atul
 */
public class XMLUtil {
    public static Document parseXMLToDocument(String filePath)
    {
        SAXBuilder builder = new SAXBuilder();
        Document document=null;
        try{
         InputStream input = new FileInputStream(filePath);     
         document = (Document) builder.build(input);
         
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return document;
    }
    
    public static List<Section> getReviewSectionListFromFile(String path)
    {
            List<Section> sectionList = new ArrayList<Section>();
            Document document =  parseXMLToDocument(path);
	    sectionList = getReviewSectionList(document);
            return sectionList;
    }
    
     public static List<Section> getReviewSectionListFromXLMStr(String xmlStr)
    {
            List<Section> sectionList = new ArrayList<Section>();
            Document document =  parseStrToDocument(xmlStr);
	    sectionList = getReviewSectionList(document);
            return sectionList;
    }
    
    public static Document parseStrToDocument(String xmlStr)
    {
        SAXBuilder builder = new SAXBuilder();
        
        Document document=null;
        try{ 
         
         document = (Document) builder.build(new StringReader(xmlStr));
         
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return document;
    }
    
    public static List<Section> getReviewSectionList(Document document)
    {
        List<Section> sectionList = new ArrayList<Section>();
           try{ 
               if(document == null) return sectionList;
		Element root = document.getRootElement();
		List sections = root.getChildren("section");
               
		for (int i = 0; i < sections.size(); i++) {
 
		   Element node = (Element) sections.get(i);
                   String orderStr = (String)node.getAttributeValue("order");
                   int order = new Integer(orderStr).intValue();
                   
                   List ratings  = node.getChild("ratings").getChildren();
                   List<Rating> ratingList = new ArrayList<Rating>();
                   for (int j = 0; j < ratings.size(); j++) {
                        Element rating = (Element) ratings.get(j);
                        String caption = (String)rating.getAttributeValue("caption");
                        String valueStr = (String)rating.getAttributeValue("value");
                       
                        Rating ratingObj = new Rating(caption,valueStr);
                        ratingList.add(ratingObj);
                        
                   }
                   // Headings
                   List headings  = node.getChild("headings").getChildren();
                   List<Heading> headingList = new ArrayList<Heading>();
                   for (int j = 0; j < headings.size(); j++) {
                        Element heading = (Element) headings.get(j);
                        String ratingStr = (String)heading.getAttributeValue("rating");
                        String idStr = (String)heading.getAttributeValue("id");
                        String valueStr = (String)heading.getValue();
                   
                        int ratingInt =0;
       
                        if(ratingStr!=null && !ratingStr.equals(""))
                            ratingInt = new Integer(ratingStr).intValue();
                
                        Heading headingObj = new Heading(valueStr,ratingInt,idStr);
                        
                        headingList.add(headingObj);
                   }
                   
                   Section section = new Section();
                   section.setHeadings(headingList);
                   section.setRatings(ratingList);
                   section.setOrder(order);
                   sectionList.add(section);	  
                   
		}
            }catch(Exception e){
                    e.printStackTrace();
            }
            return sectionList;
    }
    
}
